package friot.service.cloud.controller;

import friot.model.iot.entity.IotModel;
import friot.model.iot.entity.IotProduct;
import friot.service.cloud.service.IotProductService;
import friot.utils.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author He Kuntao
 * @desc Iot产品原型控制类
 * @date 2024/2/19 10:38
 */
@Slf4j
@RestController
@RequestMapping("/api/iot/product")
public class IotProductController {
    @Resource
    private IotProductService iotProductService;

    /**
     * 获取产品原型列表
     */
    @GetMapping
    public Result listProduct(@RequestParam(required = false, defaultValue = "false")
                              Boolean status) {
        List<IotProduct> iotProductList = iotProductService.listProduct(status);
        return Result.success(iotProductList);
    }

    /**
     * 获取指定ID产品原型详情
     *
     * @param productId 产品原型ID
     */
    @GetMapping("/{id}")
    public Result getProduct(@PathVariable("id") Integer productId) {
        IotProduct iotProduct = iotProductService.getById(productId);
        return Result.success(iotProduct);
    }


    /**
     * 创建产品原型
     *
     * @param iotProduct 产品原型实体
     */
    @PostMapping
    public Result createProduct(@RequestBody IotProduct iotProduct) {
        iotProductService.saveProduct(iotProduct);
        return Result.success();
    }

    /**
     * 产品原型绑定数据模型
     *
     * @param productId 产品原型ID
     * @param modelList 数据模型ID列表
     */
    @PostMapping("/bind/{productId}")
    public Result bindDataModel(@PathVariable Integer productId, @RequestBody ArrayList<Integer> modelList) {
        iotProductService.bindModel(productId, modelList);
        return Result.success();
    }

    /**
     * 获取产品原型绑定数据模型
     *
     * @param productId 产品原型ID
     */
    @GetMapping("/bindModels")
    public Result getBindModels(@RequestParam Integer productId) {
        List<IotModel> modelList = iotProductService.getBindModels(productId);
        return Result.success(modelList);
    }
}
